/*
 * Created by JFormDesigner on Mon Jun 19 14:10:06 CST 2023
 */

package fm.report;

import java.awt.event.*;
import dao.Dao_reportOut;
import dao.Dao_reportIn;
import model.Report_in;
import model.Report_out;

import java.awt.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class Report_trade extends JInternalFrame {
    private String [] columns = {"出库明细编号", "商品","类型", "出库数量", "出库时间", "剩余库存"};
    private String [] column = {"入库明细编号", "商品", "类型", "入库数量", "入库时间", "剩余库存"};

    private Object [][] getFS(List list)
    {
        Object [][] results = new Object[list.size()][columns.length];

        for(int i = 0; i < list.size(); i++)
        {
            Report_out report_out =  (Report_out) list.get(i);

            results[i][0] = report_out.getO_no();
            results[i][1] = report_out.getC_name();
            results[i][2] = report_out.getCs_name();
            results[i][3] = report_out.getO_number();
            results[i][4] = report_out.getO_time();
            results[i][5] = report_out.getNumber();
        }
        return results;
    }

    private Object [][] getFS_in(List list)
    {
        Object [][] results = new Object[list.size()][column.length];

        for(int i = 0; i < list.size(); i++)
        {
            Report_in report_in =  (Report_in) list.get(i);

            results[i][0] = report_in.getI_no();
            results[i][1] = report_in.getC_name();
            results[i][2] = report_in.getCs_name();
            results[i][3] = report_in.getI_number();
            results[i][4] = report_in.getI_time();
            results[i][5] = report_in.getNumber();
        }
        return results;
    }
    public Report_trade() {
        initComponents();
        iniDate();
    }

    public void iniDate()
    {
        Vector<String> its = Dao_reportOut.getTime();
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(its);
        outComboBox.setModel(model);

        Vector<String> it = Dao_reportIn.getTime();
        DefaultComboBoxModel<String> models = new DefaultComboBoxModel<>(it);
        inComboBox.setModel(models);

        String outTime = (String) outComboBox.getSelectedItem();

        Object [][] results = getFS(Dao_reportOut.select_out(outTime));
        DefaultTableModel model_1 = new DefaultTableModel(results, columns);
        outTable.setModel(model_1);

        String inTime = (String) inComboBox.getSelectedItem();

        Object [][] result = getFS_in(Dao_reportIn.select_in(inTime));
        DefaultTableModel model_2 = new DefaultTableModel(result, column);
        inTable.setModel(model_2);
    }

    private void cancel(ActionEvent e) {
        doDefaultCloseAction();
    }

    private void out(ActionEvent e) {
        String time = (String) outComboBox.getSelectedItem();

        Object [][] results = getFS(Dao_reportOut.select_out(time));
        DefaultTableModel model_1 = new DefaultTableModel(results, columns);
        outTable.setModel(model_1);
    }

    private void In(ActionEvent e) {
        String time = (String) inComboBox.getSelectedItem();

        Object [][] result = getFS_in(Dao_reportIn.select_in(time));
        DefaultTableModel model_2 = new DefaultTableModel(result, column);
        inTable.setModel(model_2);
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        cancelButton = new JButton();
        tabbedPane1 = new JTabbedPane();
        panel2 = new JPanel();
        outComboBox = new JComboBox();
        scrollPane1 = new JScrollPane();
        outTable = new JTable();
        panel3 = new JPanel();
        inComboBox = new JComboBox();
        scrollPane2 = new JScrollPane();
        inTable = new JTable();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("\u5e93\u5b58\u62a5\u8868");
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setMinimumSize(new Dimension(322, 40));
            panel1.setPreferredSize(new Dimension(322, 40));
            panel1.setLayout(null);

            //---- cancelButton ----
            cancelButton.setText("\u8fd4\u56de");
            cancelButton.addActionListener(e -> cancel(e));
            panel1.add(cancelButton);
            cancelButton.setBounds(new Rectangle(new Point(271, 5), cancelButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1, BorderLayout.SOUTH);

        //======== tabbedPane1 ========
        {

            //======== panel2 ========
            {
                panel2.setLayout(new BorderLayout());

                //---- outComboBox ----
                outComboBox.addActionListener(e -> out(e));
                panel2.add(outComboBox, BorderLayout.SOUTH);

                //======== scrollPane1 ========
                {
                    scrollPane1.setViewportView(outTable);
                }
                panel2.add(scrollPane1, BorderLayout.CENTER);
            }
            tabbedPane1.addTab("\u51fa\u5e93\u62a5\u8868", panel2);

            //======== panel3 ========
            {
                panel3.setLayout(new BorderLayout());

                //---- inComboBox ----
                inComboBox.addActionListener(e -> In(e));
                panel3.add(inComboBox, BorderLayout.SOUTH);

                //======== scrollPane2 ========
                {
                    scrollPane2.setViewportView(inTable);
                }
                panel3.add(scrollPane2, BorderLayout.CENTER);
            }
            tabbedPane1.addTab("\u5165\u5e93\u62a5\u8868", panel3);
        }
        contentPane.add(tabbedPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton cancelButton;
    private JTabbedPane tabbedPane1;
    private JPanel panel2;
    private JComboBox outComboBox;
    private JScrollPane scrollPane1;
    private JTable outTable;
    private JPanel panel3;
    private JComboBox inComboBox;
    private JScrollPane scrollPane2;
    private JTable inTable;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
